package Server;


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
import database.manipulation.DataBaseManipulation;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author labib
 */
public class chatServerImp extends UnicastRemoteObject implements charServerInt
{

    Vector<CList> all=new Vector<CList>();
     DataBaseManipulation dbm;
    public chatServerImp() throws RemoteException
    {
      dbm=new DataBaseManipulation();
    }
    public void tellOther(String message) {
//        try{
//        System.out.println(" message recieved ");
//
//        for(clientInt c:all)
//        {
//
//            c.reciev(message);
//        }
//
//        }
//        catch(Exception v)
//        {
//            System.out.println("can not send ");
//            v.printStackTrace();
//        }
//
//
    }

    public void register(clientInt cliRef,String status,String name) throws RemoteException {
        all.add(new CList(name,cliRef,status));

        dbm.changeUserStatus(name,status);
        System.out.println(" client added");
    }

    public void Unregister(clientInt cliRef,String name) throws RemoteException {
        all.remove(new CList(name,cliRef,""));
        dbm.changeUserStatus(name,"offline");
        System.out.println("client removed ");
    }

    public Vector<CList> sendList(String name) throws RemoteException {
        Vector<CList> cl=new Vector<CList>();
        String s[]=dbm.getContacts(name);
        for (int i = 0; i < s.length; i++) {
            String r=dbm.getUserStatus(s[i]);
            if(!r.equals("offline")){
                for (int j = 0; j < all.capacity(); j++) {
                    if(all.elementAt(j).getName().equals(s[i])){
                        cl.add(new CList(s[i],all.elementAt(j).getObj(),r));
                    break;
                    }

                }
           //
            }else{
                cl.add(new CList(s[i],null,r));

            }
        }
        return cl;
    }

    public String getOffMes() throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setOffMes(String msg) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   
    
    public void setStatus(String umail, String stat) throws RemoteException {

        DataBaseManipulation data =  new DataBaseManipulation();
        data.changeUserStatus(umail, stat);
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void FriendOp(String umail, String mail, int i) throws RemoteException {
        DataBaseManipulation data =  new DataBaseManipulation();

        switch (i)
        {

            case 0 :
            data.delContact(umail, mail);
            break;
            case 1 :
            data.addContact(umail, mail);
            break;

            case 2 :
            data.blockContact(umail, mail);
            break;

        }





    }

    @Override
    public int valLogin(String umail, String pass) throws RemoteException {
        DataBaseManipulation dm = new DataBaseManipulation();
        int r=dm.validateLogin(umail, pass);
        return r;
    }
    
    
}
